package com.object.module.cbb.service.impl;

import com.object.configuration.utils.ImageUtil;
import com.object.configuration.utils.PasswordEncryp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.configuration.utils.PageUtils;
import com.object.configuration.utils.Query;

import com.object.dao.cbb.CbbUserDao;
import com.object.module.cbb.entity.CbbUserEntity;
import com.object.module.cbb.service.CbbUserService;


@Service("cbbUserService")
public class CbbUserServiceImpl extends ServiceImpl<CbbUserDao, CbbUserEntity> implements CbbUserService {

    @Autowired
    private CbbUserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CbbUserEntity> page = this.page(
                new Query<CbbUserEntity>().getPage(params),
                new QueryWrapper<CbbUserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public CbbUserEntity loginUser(CbbUserEntity user) {
        String urUsername = user.getUrUsername();
        QueryWrapper<CbbUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("ur_username", urUsername);
        CbbUserEntity userEntity = getOne(wrapper);
        String urPassword = userEntity.getUrPassword();
        boolean b = PasswordEncryp.IfPassowrd(user.getUrPassword(), urPassword);
        return b ? userEntity : null;
    }

    @Override
    public boolean updateUser(CbbUserEntity cbbUser) {
        boolean flag = userDao.updateUser(cbbUser);
        return flag;
    }

    /**
     * 根据用户id对应的
     *
     * @param urId
     * @return
     */
    @Override
    public String headPortrait(Integer urId) {
        CbbUserEntity user = getById(urId);
        String avatar = user.getUrAvatar();
        String avatarUrl = ImageUtil.PictureUpdateIpAddress(avatar);
        user.setUrId(urId);
        updateById(user);
        return avatarUrl;
    }

}